package com.quanquan.management.controller;

import base.BaseConst;
import com.quanquan.management.dto.CommercialDTO;
import com.quanquan.management.dto.IntegralRuleDTO;
import com.quanquan.management.request.IntegralRuleRequestBody;
import com.quanquan.management.service.CommercialService;
import com.quanquan.management.service.IntegralRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:IntegralRuleController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/5/8 13:22
 * @Version 1.0
 */
@Controller
@RequestMapping("integral_rule")
public class IntegralRuleController extends ManagerBaseController{

    private final IntegralRuleService integralRuleService;
    private final CommercialService commercialService;

    @Autowired
    public IntegralRuleController(IntegralRuleService integralRuleService, CommercialService commercialService) {
        this.integralRuleService = integralRuleService;
        this.commercialService = commercialService;
    }

    /**
     * 查看列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(Model model) {
        List<IntegralRuleDTO> list = integralRuleService.list();
        model.addAttribute("pageObj",list);
        return "integralRule/list";
    }

    /**
     * 编辑
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(IntegralRuleRequestBody requestBody, Model model) {
        List<CommercialDTO> commercialDTOList = commercialService.list();
        model.addAttribute("commercials",commercialDTOList);
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            IntegralRuleDTO byId = integralRuleService.getById(requestBody.getId());
            model.addAttribute("editable",byId);
            return "integralRule/form";
        }
        boolean add = integralRuleService.update(requestBody);
        if (add) {
            return "redirect:/integral_rule/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","添加失败");
            model.addAllAttributes(result);
            return null;
        }
    }

}
